package com.gitee.wsl.ext.collection

fun <T> Iterable<T>.mostFrequent(): T {
    val result = groupingBy { it }
        .eachCount().entries
    if (result.isEmpty()) throw NoSuchElementException()
    return result.maxBy {
        it.value
    }.key
}

fun <T> Iterable<T>.mostFrequentOrNull(): T? {
    return groupingBy { it }
        .eachCount()
        .entries
        .maxByOrNull {
            it.value
        }?.key
}